package com.qf.controller;

import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.qf.entity.Hotal;
import com.qf.entity.R;
import com.qf.entity.Room;
import com.qf.feign.HotalFeign;
import com.qf.feign.RoomFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

@Controller
@RequestMapping("/system/export")
public class ExcelController {
    @Autowired
    private HotalFeign hotalFeign;
    @Autowired
    private RoomFeign roomFeign;
    @RequestMapping("/exp")
    public void exportById(@RequestParam Integer[] hids, HttpServletResponse response,@RequestParam String checked){
        Hotal hotal = null;
        String fileName = "hotal"+ UUID.randomUUID()+".xls";
        List<Hotal> list1 = new ArrayList<>();
        List<Room> list2 = new ArrayList<>();
        List<Map<String,Object>> expList = new ArrayList<>();
        if (checked == "one"){
            for (int i =0;i<hids.length;i++){
                Integer hid = hids[i];
                R<Hotal> hotalR = hotalFeign.queryOne(hid);
                hotal = hotalR.getData();
                R<List<Room>> roomR = roomFeign.List(hid);
                List<Room> rooms = roomR.getData();
                list1.add(hotal);
                for (int j = 0;j<rooms.size();j++){
                    Integer id = rooms.get(j).getId();
                    Integer hid1 = rooms.get(j).getHid();
                    String title = rooms.get(j).getTitle();
                    String area = rooms.get(j).getArea();
                    Integer num = rooms.get(j).getNum();
                    String info = rooms.get(j).getInfo();
                    String bed = rooms.get(j).getBed();
                    Integer number = rooms.get(j).getNumber();
                    double price = rooms.get(j).getPrice();
                    Room room = new Room();
                    room.setId(id);
                    room.setHid(hid1);
                    room.setTitle(title);
                    room.setArea(area);
                    room.setNum(num);
                    room.setInfo(info);
                    room.setBed(bed);
                    room.setNumber(number);
                    room.setPrice(price);
                    list2.add(room);
                }
            }
            ExportParams exportParams1 = new ExportParams();
            exportParams1.setSheetName("酒店列表");
            ExportParams exportParams2 = new ExportParams();
            exportParams2.setSheetName("客房列表");
            Map<String,Object> hotalMap = new LinkedHashMap<>();
            hotalMap.put("title",exportParams1);
            hotalMap.put("entity",Hotal.class);
            hotalMap.put("data",list1);
            Map<String,Object> roomMap = new LinkedHashMap<>();
            roomMap.put("title",exportParams2);
            roomMap.put("entity",Room.class);
            roomMap.put("data",list2);
            expList.add(hotalMap);
            expList.add(roomMap);
            ExcelUtil.exportExcel(expList,fileName,response);
            System.out.println("导出成功");
        }
        else{
            for (int i =0;i<hids.length;i++){
                Integer hid = hids[i];
                R<Hotal> hotalR = hotalFeign.queryOne(hid);
                hotal = hotalR.getData();
                R<List<Room>> roomR = roomFeign.List(hid);
                List<Room> rooms = roomR.getData();
                list1.add(hotal);
                for (int j = 0;j<rooms.size();j++){
                    Integer id = rooms.get(j).getId();
                    Integer hid1 = rooms.get(j).getHid();
                    String title = rooms.get(j).getTitle();
                    String area = rooms.get(j).getArea();
                    Integer num = rooms.get(j).getNum();
                    String info = rooms.get(j).getInfo();
                    String bed = rooms.get(j).getBed();
                    Integer number = rooms.get(j).getNumber();
                    double price = rooms.get(j).getPrice();
                    Room room = new Room();
                    room.setId(id);
                    room.setHid(hid1);
                    room.setTitle(title);
                    room.setArea(area);
                    room.setNum(num);
                    room.setInfo(info);
                    room.setBed(bed);
                    room.setNumber(number);
                    room.setPrice(price);
                    list2.add(room);
                }
            }
            ExportParams exportParams1 = new ExportParams();
            exportParams1.setSheetName("酒店列表");
            ExportParams exportParams2 = new ExportParams();
            exportParams2.setSheetName("客房列表");
            Map<String,Object> hotalMap = new LinkedHashMap<>();
            hotalMap.put("title",exportParams1);
            hotalMap.put("entity",Hotal.class);
            hotalMap.put("data",list1);
            Map<String,Object> roomMap = new LinkedHashMap<>();
            roomMap.put("title",exportParams2);
            roomMap.put("entity",Room.class);
            roomMap.put("data",list2);
            expList.add(hotalMap);
            expList.add(roomMap);
            ExcelUtil.exportExcel(expList,fileName,response);
            System.out.println("导出成功");
        }
    }
    @RequestMapping("/imp")
    public void importById(@RequestParam Integer[] hids, HttpServletResponse response){

        System.out.println("导入成功");
    }
}
